<?php

/*                                                                        *
 * This script is part of the TYPO3 project - inspiring people to share!  *
 *                                                                        *
 * TYPO3 is free software; you can redistribute it and/or modify it under *
 * the terms of the GNU General Public License version 2 as published by  *
 * the Free Software Foundation.                                          *
 *                                                                        *
 * This script is distributed in the hope that it will be useful, but     *
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *	
 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General      *
 * Public License for more details.                                       *
 *                                                                        */	

/**
 * @package auxo
 * @subpackage Validator
 * @version $Id$
 */

/**
 * The Contraint Class
 * 
 * @package auxo
 * @subpackage Validator
 * @copyright Copyright belongs to the respective authors
 * @author AHN
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License, version 2
 */
 
class tx_auxo_constraint {
	// operators
	const NOT_EQUAL       = 'notEqual';
	const EQUAL 		  = 'equal';
	const GREATER         = 'greater';
	const GREATER_EQUAL   = 'greaterEqual';
	const LESS			  = 'less';
	const LESS_EQUAL      = 'lessEqual'; 
	const CUSTOM 	  	  = 'custom';
	
	protected $rules  = array();
	protected $errors = NULL;
	
	public function __construct($a, $b, $message, $function = 'equal') {
		$this->rules[] = array( 
			'a' => $a,
			'b' => $b,
			'function' => $function,
			'message' => $message
		);
	}
	
	public function getErrors() {
		return $this->errors;
	}
	
	public function getValueFor($data, $required) {
		if ($required instanceof tx_auxo_constraint) {
			return $required->isValid($data);	
		}
		return $data->get($required); 
	}
	
	public function isValid($data) {
		if (!$data instanceof tx_auxo_arrayObject) {
			throw new tx_auxo_coreException('no array object given');
		}
		
		foreach ($this->rules as $rule) {
		    $a = $this->getValueFor($data, $rule['a']);
		    $b = $this->getValueFor($data, $rule['b']);					
			if (! call_user_func(array($this, $rule['function']), $a, $b)) {
			    $this->errors[] = $rule['message'];
			}
		}
		
		return $this->errors ? false : true;
	}

	protected function equal($a, $b) {
		return $a == $b;
	}

	protected function notEqual($a, $b) {
		return $a <> $b;
	}

	protected function less($a, $b) {
		return $a < $b;
	}
	
	protected function lessEqual($a, $b) {
		return $a <= $b;
	}
	
	protected function greater($a, $b) {
		return $a > $b;
	}

	protected function greaterEqual($a, $b) {
		return $a >= $b;
	}
	
}



?>